import { Component, Emit, Prop, Watch } from 'vue-facing-decorator'
import Render from '@/render/Render'
import toNative from '@/utils/toNative'
import type { FormModalDto } from '@/components/modal/Modal'
import renderModal, { getModelValue } from '@/components/modal/Modal'
import { auditOccupation } from '@/api/occupationAudit'
import { message } from 'ant-design-vue'
import { OccupationAuditStatusCnEnum } from '@/const/enum'

@Component
export class DialogReject extends Render {
    @Prop() show!: boolean
    @Prop() id!: number

    @Emit('update:show')
    emitShow(val: boolean) {
        return val
    }

    @Emit('refresh')
    handleRefresh() {}

    @Watch('show', { immediate: true })
    async handleWatchShow() {
        this.basicFormConfig.show = this.show
    }

    basicFormConfig = this.initBasicFormConfig() as FormModalDto
    initBasicFormConfig() {
        return {
            title: '驳回',
            width: '400px',
            theme: 'light',
            show: false,
            items: {
                auditRemark: {
                    name: '驳回理由',
                    element: 'textarea'
                }
            },
            complete: async () => {
                const data = getModelValue(this.basicFormConfig)
                if (!data.auditRemark?.trim()) {
                    message.error('请输入驳回理由')
                    return
                }
                await auditOccupation({
                    id: this.id,
                    auditStatus: OccupationAuditStatusCnEnum.审核驳回,
                    ...data
                })
                message.success('操作成功')
                this.handleRefresh()
                this.basicFormConfig.show = false
            },
            cancel: () => {
                this.basicFormConfig.show = false
            },
            init: () => {}
        } as FormModalDto
    }

    render() {
        return <>{renderModal(this.basicFormConfig)}</>
    }
}

export default toNative<
    {
        id: number
    },
    { refresh: () => void }
        >(DialogReject)
